﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.Mvc;
using Dogshow.Model;
using Dogshow.Web.Services;
using DogshowRepository;
using Ninject;

namespace Dogshow.Web.Controllers
{
    public class RasePasaController : Controller
    {
        private readonly IRepoServices m_Repository;

        public RasePasaController(IRepoServices repository)
        {
            m_Repository = repository;
        }

        public ActionResult Index()
        {
            var rase = m_Repository.VratiSveRasePasa();
            HttpContext.Application["RasePasa"] = rase;
            ViewBag.RasePasa = rase;
            return View();
        }

        public PartialViewResult VratiRasePasaPartial()
        {
            var rasePasa = HttpContext.Application["RasePasa"];
            return PartialView("RasePasaGridView", rasePasa);
        }

        public PartialViewResult AzurirajRasuPsa([ModelBinder(typeof(DevExpressEditorsBinder))] RasaPsa rasa)
        {
            m_Repository.SacuvajRasePasa(new[]{rasa});
            var rase = m_Repository.VratiSveRasePasa();
            HttpContext.Application["RasePasa"] = rase;
            return PartialView("RasePasaGridView", rase);
        }

        public PartialViewResult RasePasaComboBoxCallback()
        {
            return PartialView("_RasaPsaComboBox");
        }

        public static object FiltrirajRasuPoDeluNaziva(ListEditItemsRequestedByFilterConditionEventArgs args)
        {
            var repository = ((IKernel) Bootstrap.Bootstrapper.Container).Get<IRaseRepository>();
            return repository.FiltrirajPoDeluNaziva(args.Filter, args.BeginIndex, args.EndIndex);
        }

        public static object FiltrirajRasuPoId(ListEditItemRequestedByValueEventArgs args)
        {
            if (args.Value == null) return null;
            var repository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IRaseRepository>();
            int id = Convert.ToInt32(args.Value);
            return repository.VratiPoId(id);
        }

        public static IEnumerable<RasaPsa> VratiRasuPsaPoId(int? id)
        {
            if (!id.HasValue) return Enumerable.Empty<RasaPsa>();
            var repository = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IRaseRepository>();
            return new List<RasaPsa> {repository.VratiPoId(id.Value)};
        }
    }
}
